﻿Public Class frmNewCategories
    Dim Strsql As String
    Dim _mysql As New ClsSQLhelper
#Region "Function"
    Sub Load_AutoComplete_CategoriesName(Optional ByVal TypeCode As String = "%", Optional ByVal KindCode As String = "%", Optional ByVal Attribute As String = "%")
        Try
            Strsql = "Select Cate_Name from categories"
            Dim DT_Category As New DataTable
            DT_Category = _mysql.GetMYSQLDataTable(Strsql, "categories")
            If DT_Category.Rows.Count = 0 Then Exit Sub
            Dim ColItemName As New AutoCompleteStringCollection
            For i = 0 To DT_Category.Rows.Count - 1
                ColItemName.Add(DT_Category.Rows(i).Item("Cate_Name"))
            Next

            With TxtCategoriesName
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.CustomSource
                .AutoCompleteCustomSource = ColItemName

            End With


        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Load_AutoComplete_CategoriesName"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub
    Function Get_NEW_ID() As Integer
        Try
            Strsql = "Select Max(Cate_ID) as cc from categories"
            Get_NEW_ID = _mysql.MySQLExecuteScalar(Strsql) + 1

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "Get_NEW_ID"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With

        End Try
    End Function
    Function Validate_Data() As Boolean
        Validate_Data = False

        Try

            If Me.TxtCategoriesCode.Text = "" Then
                MsgBox("กรุณากรอกข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical, "ไม่สามารถเพิ่มข้อมูลได้")
                Exit Function
            End If

            If Me.TxtCategoriesName.Text = "" Then
                MsgBox("กรุณากรอกข้อมูลให้ครบถ้วน", MsgBoxStyle.Critical, "ไม่สามารถเพิ่มข้อมูลได้")
                Exit Function
            End If

            Strsql = "Select count(*) as cc from categories where Cate_Name = '" & Me.TxtCategoriesName.Text & "'"
            If (_mysql.MySQLExecuteScalar(Strsql)) > 0 Then
                MsgBox("ไม่สามารถเพิ่มข้อมูลที่มีอยู่แล้วได้", MsgBoxStyle.Critical, "ไม่สามารถเพิ่มข้อมูลได้")
                Exit Function
            End If


        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
        Return True
    End Function
#End Region
    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Try

            If Validate_Data() = False Then Exit Sub

            If MsgBox("คุณต้องการเพิ่มข้อมูลใช่หรือไม่", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "กรุณายืนยันการเพิ่มข้อมูล") = MsgBoxResult.No Then Exit Sub

            Dim New_Categories As Integer = Get_NEW_ID()

            Strsql = "Insert into categories (Cate_ID,Cate_Name,Cate_Code) values (" & New_Categories & ",'" & Me.TxtCategoriesName.Text & "','" & Me.TxtCategoriesCode.Text & "')"
            If _mysql.MySQLExecute(Strsql) > 0 Then
                MsgBox("เพิ่มข้อมูลเรียบร้อยแล้ว", MsgBoxStyle.Information, "ผลการเพิ่มข้อมูล")
                Me.DialogResult = System.Windows.Forms.DialogResult.OK

            Else
                MsgBox("ขออภัยไม่สามารถเพิ่มข้อมูลได้", MsgBoxStyle.Critical, "ผลการเพิ่มข้อมูล")
                Exit Sub
            End If


            'Add Log
            Strsql = "Insert into transections_logs (trans_code,trans_datetime,trans_computer,trans_item) values (11,'" & Pn_Framework.MysqlDateTimeFormat(Now, True) & "','" & Pn_Framework.Get_Computername & "','" & New_Categories & "') "
            Select Case (_mysql.MySQLExecute(Strsql))
                Case -1

                    With frmDebug
                        .lblFormName.Text = Me.Name
                        .lblFunctionName.Text = "OK_Button_Click"
                        .MemoErr_Description.Text = "SQL Error กรุณาตรวจสอบคำสั่ง"
                        .MemoSQL.Text = Strsql
                        .ShowDialog()
                    End With
            End Select
            DialogResult = Windows.Forms.DialogResult.OK
            Me.Dispose()

            Me.Dispose()

        Catch ex As Exception
            With frmDebug
                .lblFormName.Text = Me.Name
                .lblFunctionName.Text = "OK_Button_Click"
                .MemoErr_Description.Text = ex.Message
                .MemoSQL.Text = Strsql
                .ShowDialog()
            End With
        End Try
    End Sub

    Private Sub frmNewCategories_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Load_AutoComplete_CategoriesName()
    End Sub

    Private Sub GroupControl1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles GroupControl1.Paint

    End Sub
End Class